<?php
include_once XXF_RUNEXCEL;
class Kaoshiup_ctl extends Ctl{
    function __construct(){
    }
    function main(array $xxfget) {
        $this::sessver();
        $type=!empty($_POST['type'])?$_POST['type']:false;
        $type=!empty($xxfget['type'])?$xxfget['type']:$type;
        if(!$type){
            $result=xdb::getInstance()->getrow("select * from kaoshi where status like '激活'");
            $this::show('kaoshiup',compact('result'));
        }elseif($type=="moban"){
            $cid=$xxfget['cid'];
            $cname=xdb::getInstance()->getvar("select cname from temstuclass where id=".$cid);
            $kcid=$xxfget['kcid'];
            $kcname=xdb::getInstance()->getvar("select kcname from kecheng where id=".$kcid);
            $array=array("学号","学生姓名",$kcname."_成绩");
            $ex=new RunExcel();
            $ex->createExcel();
            $ex->setHeader($array);
            //$stu=xdb::getInstance()->getarray("SELECT sname FROM stu AS s, temstu_tct AS tt, temclassteacher AS te, temstuclass AS tc WHERE s.id = tt.sid AND tt.tctid = te.id AND te.tcid = tc.id and tc.id=".$cid." GROUP BY sname ORDER BY cname",ARRAY_N);
            //$ex->setBody($stu);
            $es=$ex->getSheet();
            $stywidth=array(//表格宽度设置
                'A'=>25,'B'=>20,'C'=>25
            );
            foreach ($stywidth as $k=>$v){
                $es->getColumnDimension($k)->setWidth($v);
            }
            $ex->down($cname.$kcname."成绩");
        }elseif($type=="up"){
            $cid=$_POST['cid'];
            $cname=xdb::getInstance()->getvar("select cname from temstuclass where id=".$cid);
            $kcid=$_POST['kcid'];
            $kcname=xdb::getInstance()->getvar("select kcname from kecheng where id=".$kcid);
            $tempFile = $_FILES['file']['tmp_name'];
            $filetype=substr($_FILES['file']['name'],-3);
            if($filetype!='xls'){
                echo "文件类型不对";
            }else{
                $ex=new RunExcel();
                $ex->fromExcel($tempFile);
                $esa=$ex->getSheetArray();
                //echo $esa[0][0];
                //exit();
                if($esa[0][0]!="学号"||explode("_",$esa[0][2])[0]!=$kcname){
                    echo "模板和你的设置不同！";
                }else{
                    $kid=$_POST['kid'];
                    $array=array();
                    $array2=array();
                    for($i=1;$i<count($esa);$i++){
                        $array['kid']=$kid;
                        $array['sno']=$esa[$i][0];
                        $array['sname']=$esa[$i][1];
                        $array['cname']=$cname;
                        $array['kcname']=$kcname;
                        $array['score']=$esa[$i][2];
                        $array['uid']=XXF_UID;
                        $array2['kid']=$kid;
                        $array2['sno']=$esa[$i][0];
                        $array2['cname']=$cname;
                        $array2['kcname']=$kcname;
                        if(strlen($esa[$i][0])>1&&strlen($esa[$i][1])>0&&strlen($esa[$i][2])>0){
                            xdb::getInstance()->indb("ksscore", $array, $array2);
                        }
                    }
                    echo 1;
                }
            }
        }elseif($type=="xg"){
            $id=$_POST['id'];
            $score=$_POST['score'];
            $array=array();
            $array2=array();
            $array['score']=$score;
            $array2['id']=$id;
            xdb::getInstance()->updb("ksscore", $array, $array2);
            echo 1;
        };
    }
}